package com.sj.parksdk.fragment.profile;

import android.content.Intent;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.activity.ActivityQrCode;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.setting.Extras;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.adapter.FragmentAdapter;
import com.xuexiang.xui.widget.tabbar.TabSegment;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

@Page(name = "我的优惠券")
public class FragmentMyCoupons extends BaseFragment {
    @BindView(R2.id.tabSegment)
    TabSegment mTabSegment;
    @BindView(R2.id.contentViewPager)
    ViewPager mContentViewPager;
    @BindView(R2.id.btn_scan)
    SuperButton btnScan;

    private String[] pages = {"可使用", "已失效"};

    private FragmentAdapter<FragmentCoupons> adapter;
    private boolean first = true;

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_fragment_my_coupons;
    }

    @Override
    protected void initViews() {
        initPages();
    }


    private void initPages() {
        for (String page : pages) {
            mTabSegment.addTab(new TabSegment.Tab(page));
        }
        adapter = new FragmentAdapter<>(getChildFragmentManager());
        adapter.setFragments(getFragments());
        mContentViewPager.setAdapter(adapter);
        mContentViewPager.setCurrentItem(0, false);
        mTabSegment.setupWithViewPager(mContentViewPager, false);
        mTabSegment.setMode(TabSegment.MODE_FIXED);
    }

    private List<FragmentCoupons> getFragments() {
        List<FragmentCoupons> fragemnts = new ArrayList<>();
        fragemnts.add(FragmentCoupons.newInstance(true));
        fragemnts.add(FragmentCoupons.newInstance(false));
        return fragemnts;
    }

    @SingleClick
    @OnClick({R2.id.btn_scan})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.btn_scan) {
            ActivityQrCode.start(this, Extras.REQUEST_CODE_QRCODE_COUPONS, R.style.XQRCodeTheme_Custom);
        }
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Intent data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        if (data == null) return;
        if (requestCode != Extras.REQUEST_CODE_QRCODE_COUPONS) return;
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!first){
            updatePages();
        }
        first = false;
    }

    private void updatePages() {
        adapter.setFragments(getFragments());
        mContentViewPager.setAdapter(adapter);
        mContentViewPager.setCurrentItem(0, false);
    }
}
